package cn.tedu.note.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.note.entity.Note;
import cn.tedu.note.service.NoteService;
import cn.tedu.note.util.JsonResult;

@Controller
@RequestMapping("/note")
public class NoteController {

	@Autowired
	private NoteService noteService;

	@RequestMapping("/list.do")
	@ResponseBody
	public JsonResult<List<Map<String, Object>>> listNotes(String notebookId) {
		try {
			List<Map<String, Object>> list = noteService.listNotes(notebookId);
			return new JsonResult<List<Map<String, Object>>>(list);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<List<Map<String, Object>>>(e);
		}
	}

	@RequestMapping("/load.do")
	@ResponseBody
	public JsonResult<Note> LoadNote(String noteId) {
		try {
			Note note = noteService.loadNote(noteId);
			return new JsonResult<Note>(note);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Note>(e);
		}
	}
	@RequestMapping("/add.do")
	@ResponseBody
	public JsonResult<Note> addNote(
	        String notebookId, 
	        String userId, String title){
	    try {
	        Note note = noteService.createNote(
	            notebookId, userId, title);
	        return new JsonResult<Note>(note);
	    } catch (Exception e) {
	        e.printStackTrace();
	        return new JsonResult<Note>(e);
	    }
	}
}
